# from models.engine import db
# from models.rule import MaterialCategoryAttributeSet, MaterialAttributePool, MaterialBasicInfo, MaterialAttributeInfo
#
#
# def get_impact_encoding_attributes(category_tree_id):
#     query = (
#         db.session.query(MaterialCategoryAttributeSet, MaterialAttributePool)
#         .join(MaterialAttributePool, MaterialCategoryAttributeSet.attribute_pool_id == MaterialAttributePool.id)
#         .filter(
#             MaterialCategoryAttributeSet.category_tree_id == category_tree_id,
#             MaterialCategoryAttributeSet.impact_encoding == True
#         )
#     )
#     result = []
#     for cas, pool in query:
#         result.append({
#             "attr_pool_id": cas.attribute_pool_id,
#             "attr_name_cn": pool.attribute_name_cn,
#             "attr_name_en": pool.attribute_name_en
#         })
#     return result
#
# def build_duplicate_query(attr_list, input_values):
#     if not attr_list or not input_values:
#         return []
#
#     subqueries = []
#     for attr in attr_list:
#         expected_value = input_values.get(attr["attr_name_en"])
#         if expected_value is None:
#             return []
#
#         subq = db.session.query(MaterialAttributeInfo.material_basic_info_id).filter(
#             MaterialAttributeInfo.attribute_pool_id == attr["attr_pool_id"],
#             MaterialAttributeInfo.attribute_value == str(expected_value)
#         ).subquery()
#         subqueries.append(subq)
#
#     if not subqueries:
#         return []
#
#     main_query = db.session.query(subqueries[0].c.material_basic_info_id.label('mbi_id'))
#     for sq in subqueries[1:]:
#         main_query = main_query.intersect(db.session.query(sq.c.material_basic_info_id))
#
#     matched_mbi_ids = [row.mbi_id for row in main_query.all()]
#     if not matched_mbi_ids:
#         return []
#
#     basic_infos = db.session.query(MaterialBasicInfo).filter(
#         MaterialBasicInfo.id.in_(matched_mbi_ids)
#     ).all()
#
#     return [{"basic_info": bi} for bi in basic_infos]